let myChart; // متغير لتخزين الرسم البياني لمنع التكرار function calculateAndChart() { const principal = parseFloat(document.getElementById('amount').value); const interest = parseFloat(document.getElementById('interest').value) / 100 / 12; const payments = parseFloat(document.getElementById('years').value) * 12; const x = Math.pow(1 + interest, payments); const monthly = (principal * x * interest) / (x - 1); if (isFinite(monthly)) { const totalPayment = monthly * payments; const totalInterest = totalPayment - principal; document.getElementById('monthly-payment').innerText = monthly.toFixed(2); // تحديث الرسم البياني updateChart(principal, totalInterest); } } function updateChart(principal, interest) { const ctx = document.getElementById('loanChart').getContext('2d'); // إذا كان الرسم البياني موجوداً مسبقاً، نقوم بحذفه لإنشاء واحد جديد if (myChart) { myChart.destroy(); } myChart = new Chart(ctx, { type: 'doughnut', data: { labels: ['أصل القرض', 'إجمالي الفوائد'], datasets: [{ data: [principal, interest], backgroundColor: ['#4CAF50', '#FF5252'], borderWidth: 1 }] }, options: { responsive: true, plugins: { legend: { position: 'bottom' } } } }); }
رئيس التحرير
محمد صلاح
الاهلي ٢٦

بنكي | عروض على المشتريات

اعرف / قارن / اطلب